/*
* @Author: mmall
* @Date:   2017-05-27 17:57:49
* @Last Modified by:   Rosen
* @Last Modified time: 2017-05-28 19:48:16
*/

'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/headerHighLight/index.js');
var _mm = require('util/mm.js');
var _product = require('service/product-service.js');
var Pagination = require('util/pagination/index.js');
var templateIndex = require('./index.string');

var page = {
    data: {
        listParam: {
            condition: _mm.getUrlParam('condition') || '',
            // categoryId      : _mm.getUrlParam('categoryId') || '',
            current: _mm.getUrlParam('pageNum') || 1,
            pageSize: _mm.getUrlParam('pageSize') || 10,
        }
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadList();
    },
    bindEvent: function () {
        var _this = this;
        // 排序的点击事件
        $('.sort-item').click(function () {
            var $this = $(this);
            _this.data.listParam.current = 1;
            $this.addClass('active').siblings('.sort-item')
                .removeClass('active asc desc');
            _this.data.listParam.brand = $this.data('brand');
            // 重新加载列表
            _this.loadList();
        });

    },
    // 加载list数据
    loadList: function () {
        var _this = this,
            listHtml = '',
            listParam = this.data.listParam,
            $pListCon = $('.p-list-con');

        $pListCon.html('<div class="loading"></div>');
        // 删除参数中不必要的字段
        listParam.categoryId
            ? (delete listParam.keyword) : (delete listParam.categoryId);
        // 请求接口
        _product.getSearchHighLightList(listParam, function (res) {
            var listHtml = "";
            res.list.forEach(function (item) {
                listHtml += " <li class=\"p-item\">\n" +
                    "                <div class=\"p-img-con\">\n" +
                    "                <a class=\"link\" target=\"_blank\">" +
                    "                <img class='p-img' src="+item['imageUrl']+"\>"+
                    "                </a>" +
                    "                </div>\n" +
                    "                <div class=\"p-price-con\">\n" +
                    "                <span class=\"p-price\">" + item['price']+"</span>\n" +
                    "            </div>\n" +
                    "            <div class=\"p-price-con\">\n" +
                    "                <span class=\"p-brand\">品牌:</span>\n" +
                    "            <span class=\"p-brand\">" + item['brand'] + "</span>\n" +
                    "            </div>\n" +
                    "            <div class=\"p-name-con\">\n" +
                    "                <a class=\"p-name\" target=\"_blank\">\n" +
                    "                <span class=\"list-name\" v-html=\"asset['name']\">" + item['name'] + "</span>\n" +
                    "            </a>\n" +
                    "            </div>\n" +
                    "            </li>"
            })


            /*  listHtml = _mm.renderHtml(templateIndex, {
                  list: res.list
              });*/
            $pListCon.html(listHtml);
            var totalResultSize = res.totalResultSize;
            var totalPage = res.totalPage;
            var currentPage = res.current;
            _this.loadPagination({
                hasPreviousPage: currentPage > 1,
                prePage: currentPage - 1,

                hasNextPage: currentPage < totalPage,
                nextPage: currentPage + 1,

                pageNum: currentPage,
                pages: totalPage
            });
        }, function (errMsg) {
            _mm.errorTips(errMsg);
        });
    },
    // 加载分页信息
    loadPagination: function (pageInfo) {
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container: $('.pagination'),
            onSelectPage: function (pageNum) {
                _this.data.listParam.current = pageNum;
                _this.loadList();
            }
        }));
    }
};
$(function () {
    page.init();
})